//two为个人信息填写与修改页面，页面与添加成员页面链接

const app = getApp()
let saving = false

Page({
  data: {
    name: '',
    gender:'',
    birthday: '',
    infoId: '',
    openid: '',
    code: '',
    fileID: '',
  },

  onReady: function() {
    app.getMyInfo().then(info => {
      const {mine, _id: infoId, _openid: openid} = info
      const {name, gender, birthday, code, fileID} = mine ? mine : {}
      this.setData({name, gender, birthday, code, infoId, openid, fileID})
      // console.log(this.data)
      // 标题栏
      // if (this.data.infoId) {
      //   wx.setNavigationBarTitle({
      //     title: '修改个人信息'
      //   })
  
      // } else {
      //   wx.setNavigationBarTitle({
      //     title: '填写个人信息'
      //   })
      // }
    })
  },
  xingming(x){
    this.setData( {
      name: x.detail.value
    })
  },
  xingbie(x){
    this.setData( {
      gender: x.detail.value
    })
  },
  nianling(x){
    this.setData({
      birthday: x.detail.value
    })
  },
  onAdd() {
    if (saving){
      return
    }
    saving = true
    if (this.data.name == '') {
      // 提示框
      wx.showToast({
        icon: 'none',
        title: '姓名不能为空',
      })
      saving = false
      return
    }
    const code = app.genertCode()
    const db = wx.cloud.database()
    db.collection('information').add({
      data: {
        mine: {
          code: code,
          name: this.data.name,
          gender: this.data.gender,
          birthday: this.data.birthday
        },
      },
      success: res => {
        // 在返回结果中会包含新创建的记录的 _id
        this.setData({
          infoId: res._id,
          code,
        })
        wx.showToast({
          title: '填写个人信息成功',
        })
        console.log('[数据库] [新增记录] 成功，记录 _id: ', res._id)
        saving = false
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '填写个人信息失败'
        })
        console.error('[数据库] [新增记录] 失败：', err)
        saving = false
      }
    })
  },
  update(){
    if (saving){
      return
    }
    saving = true
    if (this.data.name == '') {
      wx.showToast({
        icon: 'none',
        title: '姓名不能为空',
      })
      saving = false
      return
    }
    console.log(this.data.code)
    const db = wx.cloud.database()
    db.collection('information').doc(this.data.infoId).update({
      data: {
        mine: {
          name: this.data.name,
          gender: this.data.gender,
          birthday: this.data.birthday
        },
      },
    })
    wx.showToast({
      title: '修改个人信息成功',
    })
    saving = false
  },

  // 上传图片
  doUpload() {
    const that = this
    // 选择图片
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success (res) {

        wx.showLoading({
          title: '上传中',
        })

        const filePath = res.tempFilePaths[0]
        
        // 上传图片
        const cloudPath = [that.data.infoId] + Date.now() + filePath.match(/\.[^.]+?$/)[0]
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: res => {
            app.globalData.fileID = res.fileID
            const db = wx.cloud.database()
            db.collection('information').doc(that.data.infoId).update({
              data: {
                mine: {
                  fileID: res.fileID
                },
              },
            })
            console.log('[上传文件] 成功：', res)
            wx.redirectTo({
              url: '../../index/index'
            })
          },
          fail: e => {
            console.error('[上传文件] 失败：', e)
            wx.showToast({
              icon: 'none',
              title: '上传失败',
            })
          },
          complete: () => {
            wx.hideLoading()
          }
        })

      },
      fail: e => {
        console.error(e)
      },
      if (res,fileID) {
        that.setData({
          fileID: res.fileID
        })
      }
    })
  },
})


// console.log("拿到的编码====", app.genertCode());